/**
 * Created with JetBrains WebStorm.
 * User: MASTER
 * Date: 1/01/07
 * Time: 1:59
 * To change this template use File | Settings | File Templates.
 */

var loginClass = {
    layout: null,
    form: null,
    widWindow: function(){
        var me = this,
            dhxWins = new dhtmlXWindows(),
            winObj;

        dhxWins.enableAutoViewport(false);
        dhxWins.attachViewportTo("layoutMain");
        dhxWins.setImagePath("/sinav5/dhtmlx/imgs/");

        winObj = dhxWins.createWindow("widLogin", 0, 0, 530, 358);
        winObj.setText("Login de Usuario");
        winObj.button('park').hide();
        winObj.button("minmax1").hide();
        winObj.button("close").disable();
        winObj.denyResize();
        winObj.setModal(1);
        winObj.centerOnScreen();

        me.layout = winObj.attachLayout('1C');
    },
    widForm: function(){
        var me = this,
            str,
            panelForm = me.layout.cells('a');

        //attachObject("loginCard");

        panelForm.hideHeader();

        str = [
            { type:"settings" , labelWidth:84, inputWidth:250, position:"absolute" },
            { type:"input", name:"user", label:"Usuario:", labelAlign:"right", validate: "NotEmpty", inputWidth:160, labelLeft:16, labelTop:50, inputLeft:103, inputTop:50  },
            { type:"input", name:"pass", label:"Contraseña:", labelAlign:"right", validate: "NotEmpty", inputWidth:160, labelLeft:16, labelTop:75, inputLeft:103, inputTop:75  },
            { type:"button", name:"clear_btn", label:"Button", value:"Limpiar", width:"75", inputWidth:75, inputLeft:108, inputTop:100  },
            { type:"button", name:"send_btn", label:"Button", value:"Aceptar", width:"75", inputWidth:75, inputLeft:188, inputTop:100  }
        ];
        me.form = panelForm.attachForm(str);

        //me.form = panelForm.attachForm('loginCard');
    },
    onSendData: function(){
        var me = this,
            data = me.form.getFormData();

        if(me.form.validate()){
            me.form.send('index.php?r=sys_sina/login', function(loader, response) {
                if(response == true){
                    document.location = 'index.php?r=sys_sina/system';
                }
                else
                    dhtmlx.alert({
                        text: response
                    });
            });
        }
        else
            dhtmlx.alert({
                text:"Error, Todos los campos de color rojo son necesarios."
            });
    },
    onEventClickButtons: function(){
        var me = this;

        me.form.attachEvent("onButtonClick", function(name) {
            switch(name){
                case 'send_btn':
                    me.onSendData();
                break;
                case 'clear_btn':
                    me.form.clear();
                    me.form.setItemFocus('user');
                break;
            }
        });
    },
    main: function(){
        var me = this;

        loginClass.widWindow();
        loginClass.widForm();
        me.form.setItemFocus('user');
        loginClass.onEventClickButtons();
    }
};